﻿using AutoMapper;
using MediatR;
using T_Rbac_APi.Application.Command.Jobs;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.JobsHandler
{
    public class JobAddHandlers : IRequestHandler<JobAddCommand, int>
    {
        private readonly IjobRep ijobRep;
        private readonly IMapper mapper;

        public JobAddHandlers(IjobRep ijobRep, IMapper mapper)
        {
            this.ijobRep = ijobRep;
            this.mapper = mapper;
        }

        public async Task<int> Handle(JobAddCommand request, CancellationToken cancellationToken)
        {
            var lista = await ijobRep.GetAloneByresult(x => !x.IsDel && x.Jname == request.Jname);
            if (lista!=null)
            {
                return await Task.FromResult(-1);
            }
            request.CreateDate = DateTime.Now;
            request.CreateBy = "小明";
            var lists = await ijobRep.GetAloneByid(request.Jpid);
            request.JsuperiorRole=lists.Jname;
            var list= mapper.Map<Job>(request);
            return await ijobRep.Add(list);
        }
    }
}
